//package com.landinfo.domain;
//
//import org.hibernate.annotations.Cache;
//import org.hibernate.annotations.CacheConcurrencyStrategy;
//import javax.persistence.Entity;
//import javax.persistence.Id;
//import javax.persistence.Table;
//import javax.persistence.Column;
package com.landinfo.domain;

//import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
//import org.hibernate.annotations.BatchSize;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * An authority (a security role) used by Spring Security.
 */
@Entity
@Table(name = "jhi_authority")
public class Authority implements Serializable {

    private static final long serialVersionUID = 1L;

    @NotNull
    @Size(min = 0, max = 50)
    @Id
    @Column(length = 50)
    private String name;

    @Column(name = "chn_name", length = 100)
    private String chnName;

    /**
     * 排序
     */
    @ApiModelProperty(value = "排序")
    @Column(name = "sorting")
    private Integer sorting;

    /**
     * 备注
     */
    @ApiModelProperty(value = "备注")
    @Column(name = "remark")
    private String remark;

    @Size(min = 0, max = 50)
    @ApiModelProperty(value = "父级ID")
    @Column(name = "parent_id", length = 50)
    private String parentId;

    //ng-zerro-antd中tree用到
    @Transient
    private String key;

    @Transient
    private String title;

    /**
     * ng-zerro-antd中tree和Cascader使用属性
     */
    @Transient
    private boolean isLeaf;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "parent_id")
    @OrderBy(value = "sorting")
    private Set<Authority> children = new LinkedHashSet<Authority>();

    //    @OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
//    @JoinColumns(value={@JoinColumn(name="parent_id",referencedColumnName="name")})   //对应关系 Pid = id
//    @JsonIgnore
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
        name = "jhi_userpermission_authority",
        uniqueConstraints = {@UniqueConstraint(name="uk_authority_id_permission_id",columnNames={"authority_id", "permission_id"})},
        joinColumns = {@JoinColumn(name = "authority_id", referencedColumnName = "name",unique=false, nullable=false, foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))},
        inverseJoinColumns = {@JoinColumn(name = "permission_id",referencedColumnName = "id",unique=false, nullable=false, foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))})
    private Set<UserPermission> permissions = new HashSet<>();


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Authority authority = (Authority) o;

        return !(name != null ? !name.equals(authority.name) : authority.name != null);
    }

    @Override
    public int hashCode() {
        return name != null ? name.hashCode() : 0;
    }

    @Override
    public String toString() {
        return "Authority{" +
            "name='" + name + '\'' +
            "}";
    }

    public String getChnName() {
        return chnName;
    }

    public void setChnName(String chnName) {
        this.chnName = chnName;
    }

    public Set<UserPermission> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<UserPermission> permissions) {
        this.permissions = permissions;
    }

    public Integer getSorting() {
        return sorting;
    }

    public void setSorting(Integer sorting) {
        this.sorting = sorting;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public Set<Authority> getChildren() {
        return children;
    }

    public void setChildren(Set<Authority> children) {
        this.children = children;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    @Transient
    public String getTitle() {
        return this.chnName;
    }

    @Transient
    public String getKey() {
        return this.name;
    }

    @Transient
    public boolean getIsLeaf() {
        return children.size() <= 0;
    }
}



//修改之前代码
//import javax.validation.constraints.NotNull;
//import javax.validation.constraints.Size;
//import java.io.Serializable;
//
///**
// * An authority (a security role) used by Spring Security.
// */
//@Entity
//@Table(name = "jhi_authority")
//@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
//public class Authority implements Serializable {
//
//    private static final long serialVersionUID = 1L;
//
//    @NotNull
//    @Size(max = 50)
//    @Id
//    @Column(length = 50)
//    private String name;
//
//    public String getName() {
//        return name;
//    }
//
//    public void setName(String name) {
//        this.name = name;
//    }
//
//    @Override
//    public boolean equals(Object o) {
//        if (this == o) {
//            return true;
//        }
//        if (o == null || getClass() != o.getClass()) {
//            return false;
//        }
//
//        Authority authority = (Authority) o;
//
//        return !(name != null ? !name.equals(authority.name) : authority.name != null);
//    }
//
//    @Override
//    public int hashCode() {
//        return name != null ? name.hashCode() : 0;
//    }
//
//    @Override
//    public String toString() {
//        return "Authority{" +
//            "name='" + name + '\'' +
//            "}";
//    }
//}
